package com.kong.service.impl;

import com.kong.common.utils.SecurityUtils;
import com.kong.entity.dto.QueryAttendanceRecordsDto;
import com.kong.entity.pojo.AttendanceRecords;
import com.kong.entity.vo.AitVo;
import com.kong.mapper.AitMapper;
import com.kong.service.IAitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

/**
 * 加油
 *
 * @author konglingfeng
 * @date 2025/11/1
 * @desc
 */
@Service
public class AitService implements IAitService {

    @Autowired
    private AitMapper aitMapper;

    @Override
    public List<AitVo> selectAttendanceRecordsList(QueryAttendanceRecordsDto queryAttendanceRecordsDto) {

        if(!SecurityUtils.hasRole("admin")){
            queryAttendanceRecordsDto.setSign(0);
            queryAttendanceRecordsDto.setUserId(SecurityUtils.getUserId());
            System.out.println("SecurityUtils.getUserId() = " + SecurityUtils.getUserId());
        }else {
            queryAttendanceRecordsDto.setSign(1);
        }
        System.out.println("queryAttendanceRecordsDto = " + queryAttendanceRecordsDto);
        List<AitVo> list = aitMapper.selectAttendanceRecordsList(queryAttendanceRecordsDto);


        return list;
    }

    @Override
    public boolean checkAttendanceRecordsExist(Long userId, Date date) {
        // 检查是否已打卡
        return aitMapper.selectAttendanceRecordsByUserIdAndDate(userId, date);

    }

    @Override
    public void insertAttendanceRecords() {
        AttendanceRecords attendanceRecords = new AttendanceRecords();
        attendanceRecords.setUserId(SecurityUtils.getUserId());
        attendanceRecords.setMorningPunch(new Date());
        aitMapper.insertAttendanceRecords(attendanceRecords);
    }

    @Override
    public void insertAttendanceAfternoonPunch(Long userId, Date date) {
        AttendanceRecords attendanceRecords = new AttendanceRecords();
        attendanceRecords.setUserId(userId);
        attendanceRecords.setAfternoonPunch(date);
        aitMapper.insertAttendanceAfternoonPunch(attendanceRecords);
    }

    @Override
    public boolean isLate(Date date) {
        // 检查是否迟到
        if (date == null) {
            return false;
        }

        // 创建只格式化时分秒的日期格式
        SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");

        // 获取当前打卡时间的时分秒字符串
        String punchTimeStr = timeFormat.format(date);

        // 设置迟到阈值的时分秒字符串（9:00:00）
        String lateThresholdStr = "090000";

        // 字符串比较（按字典序）
        return punchTimeStr.compareTo(lateThresholdStr) > 0;
    }

    @Override
    public void updateAttendanceRecordsStatus(Long userId, Date date, int i) {
        AttendanceRecords attendanceRecords = new AttendanceRecords();
        attendanceRecords.setUserId(userId);
        attendanceRecords.setRecordDate(date);
        attendanceRecords.setStatus((long) i);
        aitMapper.updateAttendanceRecords(attendanceRecords);
    }

    @Override
    public boolean isLeaveEarly(Date date) {
        // 判断是否早退 - 使用Java 8 LocalTime只判断时分秒
        if (date == null) {
            return false;
        }

        // 转换Date到LocalTime（仅包含时分秒）
        LocalTime punchTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();

        // 设置下班时间阈值（假设18:00为正常下班时间）
        LocalTime offWorkTime = LocalTime.of(18, 0, 0);

        // 比较时间，判断是否早于下班时间
        return punchTime.isBefore(offWorkTime);
    }

    /**
     * 获取本月考勤统计信息
     *
     * @return 考勤统计数据
     */
    @Override
    public Map<String, Object> getCurrentMonthAttendanceStatistics() {
        Long userId = SecurityUtils.getUserId();

        return aitMapper.getAttendanceStatisticsByMonth(userId);
    }
}
